package cn.harry.controller.wx;

import cn.harry.common.annotation.SysLog;
import cn.harry.common.api.CommonPage;
import cn.harry.common.api.CommonResult;
import cn.harry.common.enums.BusinessType;
import cn.harry.wx.entity.WxMsgTemplate;
import cn.harry.wx.form.TemplateMsgBatchForm;
import cn.harry.wx.service.TemplateMsgService;
import cn.harry.wx.service.WxMsgTemplateService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 消息模板
 *
 * @author honghh
 * Date 2021-04-08 16:19:27
 * Copyright (C) www.tech-harry.cn
 */
@Api(tags = "wx-msgtemplate => 消息模板")
@RestController
@RequestMapping("wx/msgtemplate")
public class WxMsgTemplateController {
    @Resource
    private WxMsgTemplateService wxMsgTemplateService;
    @Autowired
    private TemplateMsgService templateMsgService;
    @Autowired
    private WxMpService wxMpService;

    @ApiOperation("list => 分页获取消息模板列表")
    @PreAuthorize("@ss.hasPermi('wx:msgtemplate:list')")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<WxMsgTemplate>> list(@CookieValue String appid,WxMsgTemplate wxMsgTemplate,
                                                        @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        wxMsgTemplate.setAppid(appid);
        IPage<WxMsgTemplate> page = wxMsgTemplateService.getPage(wxMsgTemplate, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(page));
    }

    /**
     * 查询消息模板详细
     */
    @ApiOperation("{id} => 获取消息模板信息")
    @PreAuthorize("@ss.hasPermi('wx:msgtemplate:query')")
    @GetMapping(value = "/{id}")
    public CommonResult<WxMsgTemplate> getInfo(@PathVariable Long id) {
        return CommonResult.success(wxMsgTemplateService.selectById(id));
    }

    @ApiOperation("create => 新建消息模板")
    @PostMapping(value = "/create")
    public CommonResult<Integer> create(@RequestBody WxMsgTemplate wxMsgTemplate) {
        int count = wxMsgTemplateService.create(wxMsgTemplate);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("update/{id} => 修改指定消息模板")
    @PreAuthorize("@ss.hasPermi('wx:msgtemplate:edit')")
    @SysLog(title = "消息模板数据", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/update/{id}")
    public CommonResult<Integer> update(@PathVariable Long id, @RequestBody WxMsgTemplate wxMsgTemplate) {
        int count = wxMsgTemplateService.update(id, wxMsgTemplate);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }


    @ApiOperation("deleteByIds/{ids} => 删除指定消息模板")
    @PreAuthorize("@ss.hasPermi('wx:msgtemplate:remove')")
    @SysLog(title = "消息模板类型", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/deleteByIds/{ids}")
    public CommonResult<Integer> deleteByIds(@PathVariable Long[] ids) {
        int count = wxMsgTemplateService.deleteByIds(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 同步公众号模板
     */
    @PostMapping("/syncWxTemplate")
    @PreAuthorize("@ss.hasPermi('wx:msgtemplate:edit')")
    @ApiOperation(value = "同步公众号模板")
    public CommonResult syncWxTemplate(@CookieValue String appid) throws WxErrorException {
        this.wxMpService.switchoverTo(appid);
        wxMsgTemplateService.syncWxTemplate(appid);
        return CommonResult.success();
    }

    /**
     * 批量向用户发送模板消息
     * 通过用户筛选条件（一般使用标签筛选），将消息发送给数据库中所有符合筛选条件的用户
     */
    @PostMapping("/sendMsgBatch")
    @PreAuthorize("@ss.hasPermi('wx:msgtemplate:edit')")
    @ApiOperation(value = "批量向用户发送模板消息", notes = "将消息发送给数据库中所有符合筛选条件的用户")
    public CommonResult sendMsgBatch(@CookieValue String appid, @RequestBody TemplateMsgBatchForm form) {
        this.wxMpService.switchoverTo(appid);
        templateMsgService.sendMsgBatch(form, appid);
        return CommonResult.success();
    }
}
